package com.zl.data.manger;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.zl.data.db.DataBean;
import com.zl.data.db.DataBeanInfo;

/**
 * @author ZL @朱林</a>
 * @Version 1.0
 * @Description TODO
 * @date 2021/01/08  15:20
 */
public class DataProviderManger {
    public static void encode(Context mContext, String key, String value) {
        encode(mContext, key, value.getBytes());
    }

    public static String decodeString(Context mContext, String key) {
        byte[] value = decodeBytes(mContext, key);
        if (null != value && value.length > 0) {
            return new String(value);
        } else {
            return null;
        }
    }

    public static void encode(Context mContext, String key, byte[] value) {
        ContentResolver contentResolver = mContext.getContentResolver();
        Uri uri = DataBeanInfo.getContentUri();
        Cursor cursor = mContext.getContentResolver().query(uri, null, DataBeanInfo.KEY + "=?", new String[] {key}, null);
        DataBean dataBean = null;
        if (cursor != null) {
            if (cursor.moveToNext()) {
                dataBean = DataBeanInfo.getValues(cursor);
            }
            cursor.close();
        }
        ContentValues values = DataBeanInfo.putValues(new DataBean(key, value));
        if (null != dataBean && dataBean.getId() > 0) {
            contentResolver.update(uri, values, DataBeanInfo.KEY + "=?", new String[] {key});
        } else {
            contentResolver.insert(uri, values);
        }
    }

    public static byte[] decodeBytes(Context mContext, String key) {
        ContentResolver contentResolver = mContext.getContentResolver();
        Uri uri = DataBeanInfo.getContentUri();
        Cursor cursor = contentResolver.query(uri, null, DataBeanInfo.KEY + "=?", new String[] {key}, null);
        DataBean dataBean = null;
        if (cursor != null) {
            if (cursor.moveToNext()) {
                dataBean = DataBeanInfo.getValues(cursor);
            }
            cursor.close();
        }
        return dataBean.getValue();
    }

    public static void creatData(Context mContext, String key, byte[] value) {
        ContentResolver contentResolver = mContext.getContentResolver();
        Uri uri = DataBeanInfo.getContentUri();
        ContentValues values = DataBeanInfo.putValues(new DataBean(key, value));
        contentResolver.insert(uri, values);
    }

    public static void upDateData(Context mContext, String key, byte[] value) {
        ContentResolver contentResolver = mContext.getContentResolver();
        Uri uri = DataBeanInfo.getContentUri();
        ContentValues values = DataBeanInfo.putValues(new DataBean(key, value));
        contentResolver.update(uri, values, DataBeanInfo.KEY + "=?", new String[] {key});
    }

    public static void deleteData(Context mContext, String key) {
        ContentResolver contentResolver = mContext.getContentResolver();
        Uri uri = DataBeanInfo.getContentUri();
        contentResolver.delete(uri, DataBeanInfo.KEY + "=?", new String[] {key});
    }

}
